/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package uit.jee.lib.bo;

import java.util.ArrayList;
import uit.jee.lib.access.mapper.BookMapper;
import uit.jee.lib.access.mapper.CommentMapper;
import uit.jee.lib.access.mapper.ReaderMapper;
import uit.jee.lib.dto.BookDTO;
import uit.jee.lib.dto.CommentDTO;
import uit.jee.lib.dto.ReaderDTO;

/**
 *
 * @author ngloc_it
 */
public class CommentBO {

    public boolean CheckExistedComment(CommentDTO comment) throws Exception{
       CommentMapper mapper = new CommentMapper();
       return mapper.CheckExistedComment(comment);
    }

    public ArrayList<CommentDTO> GetAllComment() throws Exception{
        CommentMapper commentMapper = new CommentMapper();
        ReaderMapper readerMapper = new ReaderMapper();
        BookMapper bookMapper = new BookMapper();

        ArrayList<CommentDTO> comments = commentMapper.GetAllComment();
        int n = comments.size();
        for(int i = 0; i < n; i++){
            final CommentDTO com = comments.get(i);
            ReaderDTO reader = readerMapper.GetReaderById(com.getReaderId());
            com.setReaderName(reader.getName());

            BookDTO book = bookMapper.getBookById(com.getBookId());
            com.setBookName(book.getName());
        }
        return comments;
    }

    public ArrayList<CommentDTO> GetCommentOnBook(String bookId) throws Exception{
        CommentMapper commentMapper = new CommentMapper();
        ReaderMapper readerMapper = new ReaderMapper();
        BookMapper bookMapper = new BookMapper();

        ArrayList<CommentDTO> comments = commentMapper.GetCommentOnBook(bookId);
        int n = comments.size();
        for(int i = 0; i < n; i++){
            final CommentDTO com = comments.get(i);
            ReaderDTO reader = readerMapper.GetReaderById(com.getReaderId());
            com.setReaderName(reader.getName());

            BookDTO book = bookMapper.getBookById(com.getBookId());
            com.setBookName(book.getName());
        }
        return comments;
    }

    public ArrayList<CommentDTO> GetCommentOnBook(BookDTO book) throws Exception{
        return GetCommentOnBook(book.getBookId());
    }

    public void Insert(CommentDTO comment) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Insert(comment);
    }

    public void Insert(CommentDTO[] comments) throws Exception {
        CommentMapper mapper = new CommentMapper();
        mapper.Insert(comments);
    }

    public void Update(CommentDTO comment) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Update(comment);
    }

    public void Update(CommentDTO[] comments) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Update(comments);
    }

    public void Delete(CommentDTO comment) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Delete(comment);
    }

    public void Delete(CommentDTO[] comments) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Delete(comments);
    }

    public void Delete(String commentId) throws Exception{
        CommentMapper mapper = new CommentMapper();
        mapper.Delete(commentId);
    }
}